<?php
class Answer_Component_Controller_Question_Add extends Phpfox_Component
{
    public function process()
    {
        $categories = Phpfox::getService('marketplace.category')->getForBrowse();
		foreach($categories as $key=>$cat){
			reset($arrEx);
			$endName = '';
			$arrEx = array();
			if($cat['name'][0]=='{'){
				$arrEx = explode("var=",$cat['name']);
				$endName = substr($arrEx[1], 6, -7);
				$categories[$key]['name'] = Phpfox::getPhrase($endName);
			}
		}
		$this->template()->assign(array(
					'categories' => $categories)
		);
		$sLink = "answer";
        $aQuestion = array();
        if ($iEditId = $this->request()->get('id')) {
            $aCondition = array("q.id = ". $iEditId);
            $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
            $aQuestion = $aQuestion['data'];

            $this->template()
                ->assign(array('aForms' => $aQuestion));

        }
		
        $aValidation = array(
            'title' => 'Game Title A is not empty',
			//'title2' => 'Game Title B is not empty',
            'detail' => Phpfox::getPhrase('answer.your_questioin_detail_is_not_empty')
        );
		if(!isset($_FILES["image"])){
			 $aValidation = array(
			 	'image' => 'Game Image is not empty',
			 );
		}
		
        $oValidator = Phpfox::getLib('validator')->set(array(
                'sFormName' => 'js_form',
                'aParams' => $aValidation
            )
        );


        if ($aVals = $this->request()->getArray('val')) {
            if ($oValidator->isValid($aVals)) {
                $aVals['user_id'] = Phpfox::getUserId();
                if ($iCategory = $this->request()->getInt('SubCategory') > 0) {
                    $aVals['category_id'] = $this->request()->getInt('SubCategory');
                } else {
                    $aVals['category_id'] = $this->request()->getInt('Category');
                }
				$aVals['is_ab'] = $this->request()->getInt('is_ab');
                if ($iQuestionId = Phpfox::getService('answer.question')->saveDatabase($aVals)) {
                    $this->url()->send($sLink. ".question.view", array('id_'. $iQuestionId), $aVals['id'] == '' ? Phpfox::getPhrase('answer.ask_a_question_successfully'): Phpfox::getPhrase('answer.updated_the_question_successfully'));
                }

            }
        }
        $sCategories = "";
        $aCondition = array('c.parent_id = 0');
        $aCategories = Phpfox::getService('answer.category')->getListNoPage('c.*', $aCondition);
        $aCategories = $aCategories['data'];
        $iMainCategory = 0;
        $iSubCategory = 0;
        if (count($aQuestion) >0 ) {
            if ($aQuestion['main_category']['id'] != 0) {
                $iMainCategory = $aQuestion['main_category']['id'];
                $iSubCategory = $aQuestion['category_id'];
            }
            else {
                $iMainCategory = $aQuestion['category_id'];
            }
        }
        foreach ($aCategories as $aItem) {
            if ($iMainCategory == $aItem['id']) {
                $sCategories .= "<option value=" . $aItem['id'] . " selected=\"selected\" >" . $aItem['name'] . "</option>\n";
            }
            else {
                $sCategories .= "<option value=" . $aItem['id'] . ">" . $aItem['name'] . "</option>\n";
            }
        }

        $sSubCategories = "";
        if ($iMainCategory > 0) {
            $sSubCategories .= "<select name='SubCategory' class='SubCategory'>\n";
            $sSubCategories .= "<option value=0>" . Phpfox::getPhrase('answer.choose_sub_categories') . "</option>\n";
            $aCondition = array('c.parent_id = ' . $iMainCategory);
            $aSubCategories = Phpfox::getService('answer.category')->getListNoPage('c.id, c.name', $aCondition);
            foreach ($aSubCategories['data'] as $aVal) {
                if ($aVal['id'] == $iSubCategory) {
                    $sSubCategories .= "<option value=" . $aVal['id'] . " selected=\"selected\">" . $aVal['name'] . "</option>\n";
                }
                else {
                    $sSubCategories .= "<option value=" . $aVal['id'] . ">" . $aVal['name'] . "</option>\n";
                }

            }
            $sSubCategories.= "</select>";
        }

        $sPhrase = Phpfox::getPhrase('answer.a_question_title_must_be_more_than_20_characters',array(
            'minTitle' => Phpfox::getParam('answer.mintitle')
        ));
        $this->template()
            ->setTitle(Phpfox::getPhrase('answer.answer'))
            ->setBreadCrumb(Phpfox::getPhrase('answer.answers'), $this->url()->makeUrl('answer'))
            ->setBreadCrumb(Phpfox::getPhrase('answer.ask_a_question'), "", true)
            ->setHeader(array(
            '<script type="text/javascript">$minTitle = '. Phpfox::getParam('answer.mintitle') .';</script>',
            'answer.css' => 'module_answer',
            'actions.js' => 'module_answer'
        ))
            ->assign( array(
            'sCategories' => $sCategories,
            'sCreateJs' => $oValidator->createJS(),
            'sGetJsForm' => $oValidator->getJsForm(false),
            'sSubCategories' => $sSubCategories,
            'sPhrase' => $sPhrase
            )
        );

    }
}